from fastapi import APIRouter
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.allsubdb.mbalib.models import headers, DownClassModel
from apps.core.global_model import IdMarksModel
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo
from apps.crawler_platform.util.requestapihelper import RequestApiHelper

router = APIRouter(route_class=ContextIncludedRoute)


@router.get("/step1/down_home")
async def down_class():
    """
    下载首页 MBA智库百科:分类索引
    :return:
    """
    return_info = ReturnInfo()
    url = "https://wiki.mbalib.com/wiki/MBA智库百科:分类索引"
    marks = ["bodyContent"]
    this_header = headers.copy()
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "header": this_header,
        "url": url,
        "timeout": 30,
        "marks": marks,
        "moths": MRequest.GET,
        "middler_list": [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares]
    }
    bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
    # rrq = AioHttpRequest()
    # rrq.set_url(url) \
    #     .set_timeout(30).set_marks(marks) \
    #     .set_header(this_header) \
    #     .set_middler_list(
    #     [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares])
    # bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()


@router.post("/step2/down_class")
async def down_class(input: InputInfoModel[DownClassModel]):
    """
    下载某个分类 获取文章的列表 <br>
    id 样例： Category:世界500强  其中 Category: 为固定格式 <br>
    :return:
    """
    return_info = ReturnInfo()
    id_ = input.data.id
    pare = input.data.pare
    marks = input.data.marks
    proxy = input.proxy
    if pare:
        url = f"https://wiki.mbalib.com/w/index.php?title={id_}&{pare}"
    else:
        url = f"https://wiki.mbalib.com/wiki/{id_}"
    this_header = headers.copy()
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "header": this_header,
        "url": url,
        "timeout": 30,
        "marks": marks,
        "moths": MRequest.GET,
        "middler_list": [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares]
    }
    bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)

    # rrq = AioHttpRequest()
    # this_header = headers.copy()
    # rrq.set_url(url).set_proxy(proxy) \
    #     .set_timeout(30).set_marks(marks) \
    #     .set_header(this_header) \
    #     .set_middler_list(
    #     [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares])
    # bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()


@router.post("/step3/down_article")
async def down_article(input: InputInfoModel[IdMarksModel]):
    """
    下载文章  <br>
    makrs: firstHeading-wrap <br>
    :return:
    """
    return_info = ReturnInfo()
    id_ = input.data.id
    proxy = input.proxy
    marks = input.data.marks
    url = f"https://wiki.mbalib.com/wiki/{id_}"
    this_header = headers.copy()
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "header": this_header,
        "url": url,
        "timeout": 30,
        "marks": marks,
        "moths": MRequest.GET,
        "middler_list": [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares]
    }
    bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
    # rrq = AioHttpRequest()
    # this_header = headers.copy()
    # rrq.set_url(url).set_proxy(proxy) \
    #     .set_timeout(30).set_marks(marks) \
    #     .set_header(this_header) \
    #     .set_middler_list(
    #     [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.have_end_middlerwares])
    # bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()
